package com.pangolin.pangolinim.ui.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.app.BroadcastMsg;
import com.pangolin.pangolinim.ui.activity.UserInfoActivity;
import com.pangolin.pangolinim.utils.BundleUtil;
import com.pangolin.pangolinim.utils.GsonUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewFriendAdapter extends RecyclerView.Adapter<NewFriendAdapter.ViewHolder>{
    private List<UIContact> mContactsList;

    public NewFriendAdapter(List<UIContact> contactsList){
        mContactsList = contactsList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView ivHeader;
        TextView tvName;
        TextView tvRefuse;
        TextView tvAdded;
        Button btnAck;
        Button btnRef;
        public ViewHolder(View view){
            super(view);
            tvName = view.findViewById(R.id.tvName);
            ivHeader = view.findViewById(R.id.ivHeader);
            tvRefuse = view.findViewById(R.id.tvRefuse);
            tvAdded = view.findViewById(R.id.tvAdded);
            btnAck = view.findViewById(R.id.btnAck);
            btnRef = view.findViewById(R.id.btnRef);
        }
    }

    @Override
    public NewFriendAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_new_friend , parent , false);
        final NewFriendAdapter.ViewHolder holder = new NewFriendAdapter.ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(NewFriendAdapter.ViewHolder holder, int position) {
        UIContact contact = mContactsList.get(holder.getAbsoluteAdapterPosition());
        //设置昵称
        if(contact.getName() != null){
            holder.tvName.setText(contact.getName());
        }
        //设置头像
        if (contact.getBitHeader() != null){
            holder.ivHeader.setImageBitmap(contact.getBitHeader());
        }
        //设置状态
        switch (contact.getType()){
            case 3:
                holder.btnAck.setVisibility(View.VISIBLE);
                holder.btnRef.setVisibility(View.VISIBLE);
                break;
            case 4:
                holder.tvRefuse.setVisibility(View.VISIBLE);
                break;
            case 5:
                holder.tvAdded.setVisibility(View.VISIBLE);
                break;
            default:
        }
        //添加按钮响应
        holder.btnAck.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Map<String, Object> brocastMap = new HashMap<String, Object>();
                brocastMap.put("type" , BroadcastMsg.DATA_UI_CONFIRM);
                brocastMap.put("uid" , contact.getUid());
                brocastMap.put("result" , 1);
                BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
                Intent intent = new Intent(BroadcastMsg.CORE_ACTION);
                intent.putExtras(bundleUtil.getBundle());
                view.getContext().sendBroadcast(intent);
                holder.btnAck.setVisibility(View.GONE);
                holder.btnRef.setVisibility(View.GONE);
                holder.tvAdded.setVisibility(View.VISIBLE);
            }
        });
        //拒绝按钮响应
        holder.btnRef.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Map<String, Object> brocastMap = new HashMap<String, Object>();
                brocastMap.put("type" , BroadcastMsg.DATA_UI_CONFIRM);
                brocastMap.put("uid" , contact.getUid());
                brocastMap.put("result" , 2);
                BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
                Intent intent = new Intent(BroadcastMsg.CORE_ACTION);
                intent.putExtras(bundleUtil.getBundle());
                view.getContext().sendBroadcast(intent);
                holder.btnAck.setVisibility(View.GONE);
                holder.btnRef.setVisibility(View.GONE);
                holder.tvRefuse.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public int getItemCount(){
        return mContactsList.size();
    }
}
